#define _CRT_SECURE_NO_WARNINGS 1

#include <vector>
#include <unordered_map>

using namespace std;

class Solution {
public:
    int findMaxLength(vector<int>& nums) {
        int sum = 0, ret = 0;
        unordered_map<int, int> hash;
        hash[0] = -1;
        for (int i = 0; i < nums.size(); ++i)
        {
            int n = nums[i] > 0 ? 1 : -1;
            sum += n;
            if (hash.count(sum))
                ret = max(i - hash[sum], ret);
            else
                hash[sum] = i;
        }
        return ret;
    }
};